var React = require('react');


var Bheader = require('../widget/Bheader');
var Footer = require('../widget/Footer');
var AddressInfo = require('../widget/AddressInfo');
var ajax = require('../tools/ajax');
var Icon = require('../components/Icon');

module.exports = React.createClass({
    getInitialState: function () {
        return {
            theme:"Default",
            addressList:[],
        }
    },
    myScroll:"",

    componentDidMount: function () {
        var uid = localStorage.getItem("userId");
        this.getAddressByUid(uid);
        setTimeout(this.iscroll,500);
    },
    /**iScroll插件*/
    iscroll(){
        var p =this;
        p.myScroll = new IScroll("#addressInfoWrapper",{
            // 默认iscroll会拦截元素的默认事件处理函数，我们需要响应onClick，因此要配置
            preventDefault: false,
            // 禁止缩放
            zoom: false,
            // 支持鼠标事件，因为我开发是PC鼠标模拟的
            mouseWheel: true,
            // 滚动事件的探测灵敏度，1-3，越高越灵敏，兼容性越好，性能越差
            probeType: 3,
            // 拖拽超过上下界后出现弹射动画效果，用于实现下拉/上拉刷新
            bounce: true,
            // 展示滚动条
            scrollbars: false
        });
        p.myScroll.refresh();
        document.addEventListener('touchmove', function(){}, false);
    },

    /**根据Userid获取收货地址信息*/
    getAddressByUid:function(uid){
        var p = this;
        ajax("POST","getAddressByUid",{uid:uid},function(dt){
            if(dt && dt.success){
                var objs = dt.data;
                localStorage.setItem("addressInfo",JSON.stringify(objs));
                var addressInfo = [];
                for(var i = 0;i<objs.length;i++){
                    addressInfo.push(objs[i]);
                }
                p.refs.AddressInfo.setState({
                    addressList:addressInfo
                });
            }
        });
    },

    action(e){
        var action = e.currentTarget.getAttribute("action");
        location.href = action;
    },

    render: function () {
        var p = this;
        var orderFlag = localStorage.getItem("orderFlag");
        return (
            <div className="main"  id="main">
                <Bheader theme={p.state.theme} headr="false" title="收货地址管理" hact={orderFlag ==1 ?"#orderInfo":"#wode"}></Bheader>
                <div id="addressInfoWrapper" className="container-t wrap">
                    <div style={{minHeight:"28.66rem"}}>
                        <hr className="orderHr" />
                        <div className="AddressWrap">
                            <AddressInfo ref="AddressInfo"></AddressInfo>
                        </div>
                    </div>
                </div>
                <div className="NewAddress" onClick={this.action} action="#editAddress/0">
                    <div className="NewBtn">
                        <Icon icon="#icon-tianjia1" />
                        <span>新建收货地址</span>
                    </div>
                </div>
            </div>
        )
    }
});
